﻿using System.Collections.Generic;
using System.Text;
using ZQNB.BaseLib.Core.Models;
using ZQNB.Common;
using ZQNB.Common.Models;
using ZQNB.Common.Web;

namespace ZQNB.BaseLib.Core.Services
{
    public class SideBarMenuRenderAce : ISideBarMenuRender
    {
        //breadcrumb
        #region breadcrumb

        public string RenderBreadcurmbHtml(MenuTreeNode currentNode)
        {
            //<ul class="breadcrumb">
            //    <li>
            //        <i class="icon-home home-icon"></i>
            //        <a href="#">breadcrumb</a>
            //    </li>
            //    <li>
            //        <a href="#">To Do</a>
            //    </li>
            //    <li class="active">Current Page</li>
            //</ul>
            StringBuilder sb = new StringBuilder();
            if (currentNode == null)
            {
                return "<ul class=\"breadcrumb\"></ul>";
            }

            sb.Append("<ul class=\"breadcrumb\">");
            IList<MenuTreeNode> breadcurmbList = new List<MenuTreeNode>();

            AppendBreadcrumbs(ref breadcurmbList, currentNode);

            foreach (var link in breadcurmbList)
            {
                if (link.Href == "#")
                {
                    sb.AppendFormat("<li><i class=\"{0}\"></i><a href=\"{1}\">{2}</a></li>", link.Class, "javascript:void(0)", link.Text);
                }
                else if (CompareUrlEqual(link.Href, currentNode.Href))
                {
                    if (currentNode.Parent == null)
                    {
                        //当前页是根
                        sb.AppendFormat("<li><i class=\"{0}\"></i><a href=\"{1}\">{2}</a></li>", link.Class, "javascript:void(0)", link.Text);
                    }
                    else
                    {
                        //当前页不是根
                        sb.AppendFormat("<li><i class=\"{0}\"></i>{1}</li>", link.Class, link.Text);
                    }
                }
                else
                {
                    sb.AppendFormat("<li><i class=\"{0}\"></i><a href=\"{1}\">{2}</a></li>", link.Class, link.Href, link.Text);
                }
            }

            sb.Append("</ul>");
            return sb.ToString();
        }

        private void AppendBreadcrumbs(ref IList<MenuTreeNode> list, MenuTreeNode currentNode)
        {
            //先父亲，后孩子
            if (currentNode.Parent != null)
            {
                AppendBreadcrumbs(ref list, currentNode.Parent);
            }
            list.Add(currentNode);
        }

        #endregion

        //sitemenu
        #region sitemenu

        public string RenderSideMenuHtml(MenuTreeNode treeNode)
        {
            if (treeNode == null)
            {
                return "<ul class=\"nav nav-list\"></ul>";
            }

            string currentUrl = MvcHelper.GetCurrentUrl();
            Nbml menuUl = new Nbml();
            menuUl.Start = "<ul class=\"nav nav-list\">";
            menuUl.End = "</ul>";

            Nbml rootMenuLi = MakeMenuLi(treeNode, currentUrl, true);
            //根移动了位置
            menuUl.InnerTags.Add(rootMenuLi);
            foreach (var child in treeNode.Children)
            {
                Nbml menuLi = MakeMenuLi(child, currentUrl, false);
                //根移动了位置
                menuUl.InnerTags.Add(menuLi);
            }

            return menuUl.ToString();
        }

        private Nbml MakeMenuLi(MenuTreeNode node, string currentUrl, bool isRoot)
        {
            //根位置特殊，特殊处理
            Nbml menuLi = new Nbml();
            if (!isRoot)
            {
                foreach (var child in node.Children)
                {
                    Nbml childMenuLi = MakeMenuLi(child, currentUrl, false);
                    menuLi.InnerTags.Add(childMenuLi);
                }
            }

            bool isCurrentNode = CompareUrlEqual(currentUrl, node.Href);
            bool hasCurrentNode = false;
            bool isParent = node.Children.Count > 0;
            if (!isRoot)
            {
                if (!isCurrentNode)
                {
                    hasCurrentNode = IsCurrentUrlInTree(node, currentUrl);
                }
            }


            //<li>
            //    <a href="index.html">
            //        <i class="icon-dashboard"></i>
            //        <span class="menu-text"> Dashboard </span>
            //    </a>
            //</li>

            //<li class="active open">
            //    <a class="dropdown-toggle" href="#">
            //        <i class="icon-list"></i>
            //        <span class="menu-text"> Tables </span>
            //        <b class="arrow icon-angle-down"></b>
            //    </a>
            //    <ul class="submenu">
            //        <li class="active">
            //            <a href="tables.html">
            //                <i class="icon-double-angle-right"></i>
            //                Simple &amp; Dynamic
            //            </a>
            //        </li>
            //    </ul>
            //</li>

            //append li tag
            StringBuilder startSb = new StringBuilder();
            //startSb.Append("<li class=\"");
            //startSb.Append(isCurrentNode ? "active" : "");
            //startSb.Append(hasCurrentNode ? " open" : "");
            //startSb.Append("\">");

            startSb.Append("<li class=\"");
            if (isCurrentNode)
            {
                startSb.Append("active");
            }
            else if (hasCurrentNode)
            {
                startSb.Append("active open");
            }
            startSb.Append("\">");

            if (isRoot)
            {
                startSb.AppendFormat("<a href=\"{0}\"><i class=\"{1}\"></i>", node.Href, node.Class);
                startSb.AppendFormat("<span class=\"menu-text\">{0}</span></a>", node.Text);
            }
            else
            {
                string parentLiCss = "menu-icon fa fa-list";
                if (isParent && !string.IsNullOrWhiteSpace(node.Class))
                {
                    parentLiCss = node.Class;
                }
                //<i class="menu-icon fa fa-caret-right"></i>
                startSb.AppendFormat(
                    isParent
                        ? "<a href=\"{0}\" class=\"dropdown-toggle\"><i class=\"{2}\"></i>"
                        : "<a href=\"{0}\"><i class=\"{1}\"></i>", node.Href, "menu-icon fa fa-caret-right", parentLiCss);

                //startSb.AppendFormat(
                //    isParent
                //        ? "<span class=\"menu-text\">{0}</span><b class=\"arrow icon-angle-down\"></b></a>"
                //        : "<span class=\"menu-text\">{0}</span></a>", node.Text);

                startSb.AppendFormat(
                   isParent
                       ? "<span class=\"menu-text\">{0}</span><b class=\"arrow fa fa-angle-down\"></b></a>"
                       : "<span class=\"menu-text\">{0}</span></a>", node.Text);

                //startSb.AppendFormat(
                //isParent
                //? "<a href=\"{0}\" class=\"dropdown-toggle\"><i class=\"{1}\"></i>"
                //: "<a href=\"{0}\"><i class=\"{1}\"></i>", node.Href, node.Class);

                //startSb.AppendFormat(
                //isParent
                //    ? "<span class=\"menu-text\">{0}</span><b class=\"arrow icon-angle-down\"></b></a>"
                //    : "<span class=\"menu-text\">{0}</span></a>", node.Text);
            }


            startSb.AppendFormat(isParent ? "<ul class=\"submenu\">" : "");

            StringBuilder endSb = new StringBuilder();
            endSb.AppendFormat(isParent ? "</ul></li>" : "</li>");

            menuLi.Start = startSb.ToString();
            menuLi.End = endSb.ToString();
            return menuLi;
        }

        private bool IsCurrentUrlInTree(MenuTreeNode treeNode, string currentUrl)
        {
            bool isCurrentNode = CompareUrlEqual(currentUrl, treeNode.Href);
            if (isCurrentNode)
            {
                return true;
            }

            foreach (var child in treeNode.Children)
            {
                bool isCurrentUrlInTree = IsCurrentUrlInTree(child, currentUrl);
                if (isCurrentUrlInTree)
                {
                    return true;
                }
            }
            return false;
        }

        #endregion

        //helpers
        private bool CompareUrlEqual(string url1, string url2)
        {
            return MvcHelper.CompareUrlEqual(url1, url2);
        }
    }
}